package com.my.fo.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.my.fo.entity.MusicOrganization;
import com.my.fo.mapper.MusicOrganizationMapper;
import com.my.fo.properties.MusicOrganizationProperties;
import com.my.fo.service.MusicOrganizationService;
import com.my.fo.sse.SSEService;
import com.my.fo.strategy.factory.OrganizationExistTypeStrategyFactory;
import com.my.fo.strategy.factory.OrganizationTypeStrategyFactory;
import com.my.fo.strategy.organization.inter.OrganizationExistTypeStrategy;
import com.my.fo.strategy.organization.inter.OrganizationTypeStrategy;
import com.my.fo.util.MusicOrganizationUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.io.File;
import java.nio.file.Paths;
import java.util.List;

@Service
@RequiredArgsConstructor
public class MusicOrganizationServiceImpl extends ServiceImpl<MusicOrganizationMapper, MusicOrganization> implements MusicOrganizationService {

    private final MusicOrganizationProperties musicOrganizationProperties;
    private final SSEService sseService;

    @Async
    @Override
    public void organization(Long id) {
        MusicOrganization musicOrganization = getById(id);
        if (musicOrganization == null) return;
        List<String> inputSrcList = StrUtil.split(musicOrganization.getInputSrc(), '\n');
        if (CollectionUtil.isEmpty(inputSrcList)) return;
        String defOutput = musicOrganizationProperties.getDefOutput();
        String outputSrc = StrUtil.nullToDefault(musicOrganization.getOutputSrc(), Paths.get(inputSrcList.get(0), defOutput + "/").toString());
        String orgStruct = StrUtil.nullToDefault(musicOrganization.getOrgStruct(), musicOrganizationProperties.getDefOrgStruct());
        OrganizationExistTypeStrategy existTypeStrategy = OrganizationExistTypeStrategyFactory.getStrategy(musicOrganization.getExistType());
        OrganizationTypeStrategy organizationTypeStrategy = OrganizationTypeStrategyFactory.getStrategy(musicOrganization.getType());
        List<String> inList = musicOrganizationProperties.getInList();
            String uid = "music_organization_" + id;
        sseService.sendMessage(uid, "0", "开始整理...");
        for (String inputSrc : inputSrcList) {
            if (!FileUtil.exist(inputSrc)) {
                sseService.sendMessage(uid, "0", "【" + inputSrc + "】目录不存在，已跳过...");
                continue;
            }
            List<File> files = MusicOrganizationUtil.loopMusicFile(inputSrc, defOutput, (file) -> inList.contains(FileUtil.getSuffix(file).toLowerCase()));
            sseService.sendMessage(uid, "0", "【" + inputSrc + "】目录下共有" + files.size() + "个文件需要整理...");
            for (File file : files) {
                File outFile = FileUtil.file(MusicOrganizationUtil.getMusicFileByOrgStruct(outputSrc, orgStruct, file));
                organizationTypeStrategy.handlerType(file, outFile, existTypeStrategy);
                sseService.sendMessage(uid, "0", "已整理: " + file.getPath() + " ===> " + outFile.getPath());
            }
        }
        sseService.sendMessage(uid, "0", "整理完成...");
    }

}
